package cn.jsj.invite.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.jsj.invite.dao.InvitationDao;
import cn.jsj.invite.dao.SchoolDao;
import cn.jsj.invite.dao.StudentDao;
import cn.jsj.invite.pojo.*;
import cn.jsj.invite.service.SchoolService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolServiceImpl implements SchoolService {

    private final SchoolDao schoolDao;
    private final StudentDao studentDao;
    private final InvitationDao invitationDao;

    public SchoolServiceImpl(SchoolDao schoolDao, StudentDao studentDao, InvitationDao invitationDao) {
        this.schoolDao = schoolDao;
        this.studentDao = studentDao;
        this.invitationDao = invitationDao;
    }

    @Override
    public List<SchoolInfo> listSchoolSimpleInfo() {
        return schoolDao.selectSchoolSimpleInfo();
    }

    @Override
    public SchoolInfo getOneSchoolById(Integer id) {
        return schoolDao.selectOneSchoolById(id);
    }

    @Override
    public List<StudentEnt> listStudentsByName(Integer schoolId, String sutName) {
        return studentDao.selectStudentsByName(schoolId, sutName);
    }

    @Override
    public StudentEnt getOneStudentByNumber(Integer schoolId, String number) {
        return studentDao.selectOneStudentByNumber(schoolId, number);
    }

    @Override
    public List<StudentEnt> listStudentsByFaculty(Integer schoolId, String faculty) {
        return studentDao.selectStudentsByFaculty(schoolId, faculty);
    }

    @Override
    public Integer reviewStudentRegister(Integer schoolId, Integer schoolAdminId, Integer stuId) {
        StudentEnt studentEnt = studentDao.selectStudentById(stuId.longValue());
        SchoolInfo schoolInfo = schoolDao.selectOneSchoolById(schoolId);
        SchoolAdminEnt schoolAdminEnt = schoolDao.selectSchoolAdminById(schoolAdminId.longValue());
        //参数为空返回null。表示参数有误
        if (ObjectUtil.hasNull(schoolId, schoolAdminId, stuId)) {
            return null;
        }
        if (ObjectUtil.hasNull(studentEnt, schoolInfo, schoolAdminEnt)) {
            return -1;
        }
        //返回影响条数，1成功，0失败
        Integer studentSchoolId = schoolInfo.getId();
        if (studentSchoolId.equals(studentEnt.getSchoolId().intValue()) && studentSchoolId.equals(schoolAdminEnt.getSchoolId().intValue())) {
            return studentDao.updateOneStudentRegister(stuId);
        }
        //操作失败
        return 0;
    }

    @Override
    public Integer reviewInvitation(Integer schoolId, Integer schoolAdminId, Integer postId) {

        SchoolAdminEnt schoolAdminEnt = schoolDao.selectSchoolAdminById(schoolAdminId.longValue());
        SchoolInfo schoolInfo = schoolDao.selectOneSchoolById(schoolId);
        InvitationInfo invitation = invitationDao.selectOneInvitationById(postId);
        StudentEnt studentEnt = studentDao.selectOneStudentById(invitation.getInitiatorId());
        //参数为空
        if (ObjectUtil.hasNull(schoolId, schoolAdminId, postId)) {
            return null;
        }
        //参数无效
        if (ObjectUtil.hasNull(schoolInfo, schoolAdminEnt, invitation, studentEnt)) {
            return -1;
        }
        Integer schoolInfoId = schoolInfo.getId();
        if (schoolInfoId.equals(studentEnt.getSchoolId().intValue()) && schoolInfoId.equals(schoolAdminEnt.getSchoolId().intValue())) {
            return invitationDao.updateOneInvitationReview(postId);
        }
        return 0;
    }

    @Override
    public List<InvitationInfo> listInvitationNeedApprove(Integer schoolId, Long schoolAdminId) {
        List<InvitationInfo> invitationInfos = invitationDao.selectInvitationNeedApprove(schoolId);
        //将逗号分隔的标签和图片转换为list集合
        InvitationServiceImpl.invitationInfoDataFormat(invitationInfos);
        return invitationInfos;
    }

    @Override
    public Integer deletePageByPageId(int pageId, int schoolId, int id) {
        //首先判断该帖子是不是该学校的管理员，如果是的话，判断帖子是什么身份发的，企业直接删，学生，拿去判断学生是否属于这个学校
        List<SchoolAdminEnt> schoolAdminEnts = schoolDao.selectOneSchoolAdminById((long) schoolId);
        int schoolIdBySql = 0;
        for (SchoolAdminEnt schoolAdminEnt : schoolAdminEnts) {
            schoolIdBySql = Math.toIntExact(schoolAdminEnt.getSchoolId());
        }

        if (schoolId == schoolIdBySql) {//是学校管理员
            InvitationInfo invitationInfo = invitationDao.selectOneInvitationById(pageId);
            int initiatorIdentityId = invitationInfo.getInitiatorIdentityId();//发起人身份ID，与帖子类型有关，1学生，2学生会长，3企业
            if (initiatorIdentityId == 1 || initiatorIdentityId == 2) {//学生帖子
                int initiatorId = invitationInfo.getInitiatorId();//发起人ID
                StudentEnt studentEnt = studentDao.selectOneStudentById(initiatorId);
                int schoolIdByStudent = Math.toIntExact(studentEnt.getSchoolId());
                if (schoolId == schoolIdByStudent) {//该学生贴确实和管理员属于一个学校
                    return schoolDao.deletePageByPageId(pageId);
                }
            } else if (initiatorIdentityId == 3) {//企业帖子
                return schoolDao.deletePageByPageId(pageId);
            }
        }

        return 0;
    }

    @Override
    public Integer updatePasswordByOldPwdAndAdminId(String oldPwd, String newPwd, int adminId) {
        return schoolDao.updatePasswordByOldPwdAndAdminId(oldPwd, newPwd, adminId);
    }

    @Override
    public Integer updateSchoolAdminConcat(Integer adminId, String phone) {
        String reg = "^(1[3-9])\\d{9}$";
        if (phone.matches(reg)) {
            return schoolDao.updateSchoolAdminConcatById(adminId, phone);
        }
        return 0;
    }

    @Override
    public List<StudentEnt> listStudentNeedApprove(Integer adminId, Integer schoolId) {
        SchoolAdminEnt schoolAdminEnt = schoolDao.selectSchoolAdminById(adminId.longValue());
        if (schoolAdminEnt != null) {
            return studentDao.selectStudentNeedApprovedBySchoolId(schoolId);
        }
        return new ArrayList<>(0);

    }

    @Override
    public Integer schoolUpdatePassword(Integer stuId, String newPassword) {
        if (ObjectUtil.hasNull(stuId, newPassword) || "".equals(newPassword)) {
            return null;
        }
        return studentDao.updateStudentPassword(stuId, newPassword);
    }

    @Override
    public Integer disableStudentById(Integer stuId) {
        if (stuId == null) {
            return null;
        }
        return schoolDao.updateDisableStudentById(stuId);
    }

    @Override
    public List<InvitationInfo> schoolFindAllEnterprise(Integer schoolId) {
        if (schoolId == null || schoolId.equals(0)) {
            return null;
        }
        SchoolInfo schoolInfo = schoolDao.selectOneSchoolById(schoolId);
        if (schoolInfo == null) {
            return null;
        }
        return invitationDao.selectAllInvitationBySchoolId(schoolId);
    }

}
